Ext.ns('App.form');
App.form.AbstractForm = Ext.extend(Object, {
			clearItem : function(p) {
				if (typeof p.items != 'undefined') {
					var item = null;
					while ((item = p.items.last())) {
						p.remove(item, true);
					}
				}
			}
		});

Ext.ns('App.form');
App.form.AutoForm = Ext.extend(App.form.AbstractForm, {
			decorate : function(tabPanel, model) {
				this.clearItem(tabPanel);
				this.resetBasic(tabPanel, model);
			},

			resetBasic : function(tabPanel, model) {
				var p = new Ext.form.FormPanel({
							title : '节点配置',
							labelWidth : 90,
							labelAlign : 'right',
							border : false,
							defaultType : 'textfield',
							defaults : {
								anchor : '90%'
							},
							bodyStyle : {
								padding : '6px 0 0'
							},
							items : [{
										name : 'name',
										fieldLabel : '名称',
										value : model.text ? model.text : '',
										listeners : {
											'blur' : function(field) {
												var newValue = field.getValue();
												model.text = newValue;
												model.editPart.figure.updateAndShowText(newValue);
											}
										}
									}, {
										name : 'storeName',
										fieldLabel : '存储过程名称',
										value : model.dom.getElementContent('storeName'),
										listeners : {
											'blur' : function(field) {
												var newValue = field.getValue();
												model.dom.setElementContent('storeName', newValue);
											}
										}
									}, {
										name : 'exceptionMethod',
										fieldLabel : '异常处理方法',
										value : model.dom.getElementContent('exceptionMethod'),
										listeners : {
											'blur' : function(field) {
												var newValue = field.getValue();
												model.dom.setElementContent('exceptionMethod', newValue);
											}
										}
									}, {
										name : 'description',
										fieldLabel : '备注',
										xtype : 'textarea',
										value : model.dom.getElementContent('description'),
										listeners : {
											'blur' : function(field) {
												var newValue = field.getValue();
												model.dom.setElementContent('description', newValue);
											}
										}
									}]
						});

				tabPanel.add(p);
				tabPanel.activate(p);
			}
		});

Ext.ns('App.form');
App.form.EndForm = Ext.extend(App.form.AbstractForm, {
			decorate : function(tabPanel, model) {
				this.clearItem(tabPanel);
				this.resetBasic(tabPanel, model);
			},

			resetBasic : function(tabPanel, model) {
				var p = new Ext.form.FormPanel({
							title : '节点配置',
							labelWidth : 70,
							labelAlign : 'right',
							border : false,
							defaultType : 'textfield',
							defaults : {
								anchor : '90%'
							},
							bodyStyle : {
								padding : '6px 0 0'
							},
							items : [{
										name : 'name',
										fieldLabel : '名称',
										value : model.text ? model.text : '',
										listeners : {
											'blur' : function(field) {
												var newValue = field.getValue();
												model.text = newValue;
											}
										}
									},{
										name : 'description',
										fieldLabel : '备注',
										xtype : 'textarea',
										value : model.dom.getElementContent('description'),
										listeners : {
											'blur' : function(field) {
												var newValue = field.getValue();
												model.dom.setElementContent('description', newValue);
											}
										}
									}]
						});

				tabPanel.add(p);
				tabPanel.activate(p);
			}
		});

Ext.ns('App.form');
App.form.ForkForm = Ext.extend(App.form.AbstractForm, {
			decorate : function(tabPanel, model) {
				this.clearItem(tabPanel);
				this.resetBasic(tabPanel, model);
			},

			resetBasic : function(tabPanel, model) {
				var p = new Ext.form.FormPanel({
							title : '节点配置',
							labelWidth : 70,
							labelAlign : 'right',
							border : false,
							defaultType : 'textfield',
							defaults : {
								anchor : '90%'
							},
							bodyStyle : {
								padding : '6px 0 0'
							},
							items : [{
										name : 'name',
										fieldLabel : '名称',
										value : model.text ? model.text : '',
										listeners : {
											'blur' : function(field) {
												var newValue = field.getValue();
												model.text = newValue;
											}
										}
									}, {
										name : 'description',
										fieldLabel : '备注',
										xtype : 'textarea',
										value : model.dom.getElementContent('description'),
										listeners : {
											'blur' : function(field) {
												var newValue = field.getValue();
												model.dom.setElementContent('description', newValue);
											}
										}
									}]
						});
				tabPanel.add(p);
				tabPanel.activate(p);
			}
		});

Ext.ns('App.form');
App.form.JoinForm = Ext.extend(App.form.AbstractForm, {
			decorate : function(tabPanel, model) {
				this.clearItem(tabPanel);
				this.resetBasic(tabPanel, model);
			},

			resetBasic : function(tabPanel, model) {
				var p = new Ext.form.FormPanel({
							title : '节点配置',
							labelWidth : 70,
							labelAlign : 'right',
							border : false,
							defaultType : 'textfield',
							defaults : {
								anchor : '90%'
							},
							bodyStyle : {
								padding : '6px 0 0'
							},
							items : [{
										name : 'name',
										fieldLabel : '名称',
										value : model.text ? model.text : '',
										listeners : {
											'blur' : function(field) {
												var newValue = field.getValue();
												model.text = newValue;
											}
										}
									}, {
										name : 'multiplicity',
										fieldLabel : '汇聚数目',
										value : model.dom.getAttribute('multiplicity'),
										xtype : 'numberfield',
										listeners : {
											'blur' : function(field) {
												var newValue = field.getValue();
												model.dom.setAttribute('multiplicity', newValue);
											}
										}
									},  {
										name : 'description',
										fieldLabel : '备注',
										xtype : 'textarea',
										value : model.dom.getElementContent('description'),
										listeners : {
											'blur' : function(field) {
												var newValue = field.getValue();
												model.dom.setElementContent('description', newValue);
											}
										}
									}]
						});

				tabPanel.add(p);
				tabPanel.activate(p);
			}
		});
Ext.ns('App.form');
App.form.ProcessForm = Ext.extend(App.form.AbstractForm, {
			decorate : function(tabPanel, model) {
				this.clearItem(tabPanel);
				this.resetBasic(tabPanel, model);
			},

			resetBasic : function(tabPanel, model) {
				var p = new Ext.form.FormPanel({
							title : '流程配置',
							labelWidth : 70,
							labelAlign : 'right',
							border : false,
							defaultType : 'textfield',
							defaults : {
								anchor : '90%'
							},
							bodyStyle : {
								padding : '6px 0 0'
							},
							items : [{
										name : 'procDefName',
										fieldLabel : '流程名称',
										value : model.procDefName ? model.procDefName : '',
										xtype : 'combo',
										displayField : 'name',
										valueField : 'name',
										triggerAction : 'all',
										store : new Ext.data.Store({
													url : '../console/procver!getProcDefByProcCat.do',
													reader : new Ext.data.JsonReader({
																root : ''
															}, ['id', 'name', 'code']),
													listeners : {
														beforeload : function() {
															var form = p.getForm();
															var field = form.findField("procDefName");
															field.store.baseParams.procCatId = this.procCatId;
														},
														scope : this
													}
												}),
										listeners : {
											select : function(combo, record) {
												var form = p.getForm();
												this.procDefId = record.get('id');
												form.findField('procDefCode').setValue(record.get('code'));

												model.procDefId = record.get('id');
												model.procDefCode = record.get('code');
											},
											scope : this
										}
									}, {
										name : 'procDefCode',
										fieldLabel : '流程编码',
										value : model.procDefCode ? model.procDefCode : '',
										listeners : {
											'blur' : function(field) {
												var newValue = field.getValue();
												model.procDefCode = newValue;
											}
										}
									}, {
										name : 'descn',
										fieldLabel : '备注',
										xtype : 'textarea',
										value : model.dom.getElementContent('description'),
										listeners : {
											'blur' : function(field) {
												var newValue = field.getValue();
												model.dom.setElementContent('description', newValue);
											}
										}
									}]
						});

				tabPanel.add(p);
				tabPanel.activate(p);
			}
		});

Ext.ns('App.form');
App.form.StartForm = Ext.extend(App.form.AbstractForm, {
			decorate : function(tabPanel, model) {
				this.clearItem(tabPanel);
				this.resetBasic(tabPanel, model);
			},

			resetBasic : function(tabPanel, model) {
				var p = new Ext.form.FormPanel({
							title : '节点配置',
							labelWidth : 70,
							labelAlign : 'right',
							border : false,
							defaultType : 'textfield',
							defaults : {
								anchor : '90%'
							},
							bodyStyle : {
								padding : '6px 0 0'
							},
							items : [{
										name : 'name',
										fieldLabel : '名称',
										value : model.text ? model.text : '',
										listeners : {
											'blur' : function(field) {
												var newValue = field.getValue();
												model.text = newValue;
											}
										}
									}, {
										name : 'form',
										fieldLabel : '表单',
										value : model.dom.getAttribute('form'),
										listeners : {
											'blur' : function(field) {
												var newValue = field.getValue();
												model.dom.setAttribute('form', newValue);
											}
										}
									}, {
										name : 'description',
										fieldLabel : '备注',
										xtype : 'textarea',
										value : model.dom.getElementContent('description'),
										listeners : {
											'blur' : function(field) {
												var newValue = field.getValue();
												model.dom.setElementContent('description', newValue);
											}
										}
									}]
						});

				tabPanel.add(p);
				tabPanel.activate(p);
			}
		});

Ext.ns('App.form');
App.form.TransitionForm = Ext.extend(App.form.AbstractForm, {

			decorate : function(tabPanel, model) {
				this.clearItem(tabPanel);
				this.resetBasic(tabPanel, model);
			},

			resetBasic : function(tabPanel, model) {
				var condition = model.dom.getElementAttribute('condition', 'expr');
				if (condition != '') {
					condition = condition.replace(/&lt;/g, '<').replace(/&amp;/g, '&');
				}

				var items = [];
				items.push({
							name : 'name',
							fieldLabel : '名称',
							value : model.text ? model.text : '',
							listeners : {
								'blur' : function(field) {
									var newValue = field.getValue();
									// FIXME: use command
									model.text = newValue;
									model.editPart.figure.updateAndShowText(newValue);
								}
							}
						});

				if (model.getSource().type == 'decision') {
					items.push({
								name : 'condition',
								fieldLabel : '条件',
								// <condition expr="#{ACCOUNT&gt;=10000}"/>
								value : condition,
								listeners : {
									'blur' : function(field) {
										var newValue = field.getValue();
										if (newValue != '') {
											model.dom.setElementAttribute('condition', 'expr', newValue);
										} else if (model.dom.elements.length > 0) {
											model.dom.removeElement(model.dom.elements[0]);
										}
									}
								}
							});
				}

				items.push({
							name : 'description',
							fieldLabel : '备注',
							xtype : 'textarea',
							value : model.dom.getElementContent('description'),
							listeners : {
								'blur' : function(field) {
									var newValue = field.getValue();
									model.dom.setElementContent('description', newValue);
								}
							}
						});

				var p = new Ext.form.FormPanel({
							title : '跳转配置',
							labelWidth : 70,
							labelAlign : 'right',
							border : false,
							defaultType : 'textfield',
							defaults : {
								anchor : '90%'
							},
							bodyStyle : {
								padding : '6px 0 0'
							},
							items : items
						});

				tabPanel.add(p);
				tabPanel.activate(p);
			}

		});
